<?php
/**
* session file处理类
*/
class SessionFile extends AbsSessionHandle {
	private $data_handler = 'base64'; //以何种方式保存和解析session数据，可选值base64
	
	public function __construct($cfg) {
		parent::__construct($cfg);
	}
	
	public function open($savepath, $name) {
		return TRUE;
	}
	
	public function close() {
		return TRUE;
	}
	
	public function read($id) {
		$sess_file = $this->cfg['file_path'] . "sess_$id";
		switch($this->data_handler) {
			case 'base64':
				$data = (string) base64_decode(@file_get_contents($sess_file));
				break;
			default:
				$data = @file_get_contents($sess_file);
				break;
		}
		return $data;
	}
	
	public function write($id, $sess_data) {
		$sess_file = $this->cfg['file_path'] . "sess_$id";
		if ($fp = @fopen($sess_file, "wb")) {
			switch($this->data_handler) {
				case 'base64':
					$data = base64_encode($sess_data);
					break;
				default:
					$data = $sess_data;
					break;
			}
			$return = @fwrite($fp, $data);
			fclose($fp);
			return $return;
		} else {
			return FALSE;
		}
	}
	
	public function destroy($id) {
		$sess_file = $this->cfg['file_path'] . "sess_$id";
		return(@unlink($sess_file));
	}
	
	public function gc($maxlifetime) {
		foreach (glob($this->cfg['file_path'] . "sess_*") as $filename) {
			if (filemtime($filename) + $this->lifetime < time()) {
				@unlink($filename);
			}
		}
		return TRUE;
	}
}
?>